﻿#include "FundInfoDockWidget.h"
#include"FindFundNameThread.h"
#include"TableWindow.h"
#include"MainWindow.h"
#include"ResizeWindowEventFilter.h"
#include<QStandardItemModel>
#include<QPushButton>
#include<QButtonGroup>
#include<QFileDialog>
#include<QMessageBox>
#include<QLabel>
#include<QDebug>
FundInfoDockWidget::FundInfoDockWidget(FindFund* Fund, MainWindow* parent)
	:QDockWidget("基金基本信息", parent),
    m_FindFund(Fund)
{
    QSize buttSize(200, 50); int y = 20;
    m_Label = new QLabel("当前数据空", this);
    m_Label->move(0, y);
    m_Label->resize(buttSize);
    m_Label->setAlignment(Qt::AlignCenter);

    m_ButtonGroup = new QButtonGroup(this);
    auto ButtonConnected = new QPushButton("联网获取所有基金的基本信息", this);
    m_ButtonGroup->addButton(ButtonConnected);
    connect(ButtonConnected, &QPushButton::clicked, [=]() {ButtonFindFundSlots(ButtonConnected); });
    connect(this, &FundInfoDockWidget::sendMessage, parent, &MainWindow::showMessage);

    auto ButtonRead = new QPushButton("读取本地文件到内存", this);
    m_ButtonGroup->addButton(ButtonRead);
    connect(ButtonRead, &QPushButton::clicked, this, &FundInfoDockWidget::readFile);

    auto ButtonSave = new QPushButton("导出数据到本地文件", this);
    m_ButtonGroup->addButton(ButtonSave);
    connect(ButtonSave, &QPushButton::clicked,this,&FundInfoDockWidget::SaveFile);

    m_ButtonHidden = new QPushButton("显示数据", this);
    m_ButtonGroup->addButton(m_ButtonHidden);
    connect(m_ButtonHidden, &QPushButton::clicked, this, &FundInfoDockWidget::TableHidden);
    m_ButtonHidden->setEnabled(false);

    //按钮大小设置排序
    for (auto button : m_ButtonGroup->buttons())
    {
        y += buttSize.height();
        button->move(0, y);
        button->resize(buttSize);
    }
    installEventFilter(new ResizeWindowEventFilter([=](QResizeEvent* ev)->bool {
    for (auto button : m_ButtonGroup->buttons())
    {
        button->setFixedWidth(width());
        m_Label->setFixedWidth(width());
    }
    return true;
        }, this));
}

void FundInfoDockWidget::setButtonEnabled(const bool flag)
{
    auto child = m_ButtonGroup->buttons();
    for (auto& v : child)
    {
        auto button = (QPushButton*)v;
        button->setEnabled(flag);
    }
}

void FundInfoDockWidget::resultToTable(int val)
{
    QString str = QString("查询到%1条信息").arg(val);
    sendMessage(str);
    init();
    showTable();
}

void FundInfoDockWidget::SaveFile()
{
    if (m_FindFund->FundForm().empty())
    {
        QMessageBox::critical(this, "警告", "内存中并没有数据\r\n请先联网获取数据或导入文件");
        return;
    }
    QString FileName = QFileDialog::getSaveFileName(this, tr("保存"), "Fund/FundName.csv", tr("(*.csv)"));
    if (FileName.isEmpty())
        return;

    int n = m_FindFund->WriteFundName(FileName);
    QString str = QString("保存了%1条记录\r\n文件路径:%2").arg(n).arg(FileName);
    QMessageBox::information(this, "完成", str);
}

void FundInfoDockWidget::readFile()
{
    QString FileName = QFileDialog::getOpenFileName(this, tr("保存"), "Fund/FundName.csv", tr("(*.csv)"));
    if (FileName.isEmpty())
        return;
    sendMessage("文件读取中，请稍后");
    m_FindFund->claerFundForm();
    int n = m_FindFund->ReadFundName(FileName);
    if (n == 0)
    {
        QMessageBox::critical(this, "警告", "本次文件读取失败\r\n请确认文件是本软件导出的");
        sendMessage("文件读取失败\r\n");
        m_Label->setText("0条记录");
        m_ButtonHidden->setEnabled(false);
        return;
    }
    QString str = QString("读取了%1条记录\r\n文件路径:%2").arg(n).arg(FileName);
    //QMessageBox::information(this, "完成", str);
    sendMessage(str);
    str = QString("%1条记录").arg(n);
    m_Label->setText(str);
    m_ButtonHidden->setEnabled(true);
    showTable();
}

void FundInfoDockWidget::TableHidden()
{
    auto Table = ((MainWindow*)parent())->tableWindow();
    Table->clearTable();
    showTable();
}

void FundInfoDockWidget::showTable()
{
    auto Table = ((MainWindow*)parent())->tableWindow();
    //清空表格的数据
    Table->clearAll();
    QStringList table_h_headers;
    table_h_headers << "基金名字" << "基金代码" << "基金类型";
    Table->setHorizontalHeading(table_h_headers);//设置标题
    for (auto& data : m_FindFund->FundForm())
    {
        QStringList list;
        list << data.second.name << data.second.code << data.second.type;
        Table->insertData(list);//插入一行数据
    }
    Table->setColumnWidth();//根据窗口大小和列数自动调节列宽
    Table->setTextAlignmentAll(Qt::AlignCenter);//设置默认的字体对齐方式
    Table->initRun();//表格初始化运行
}
void FundInfoDockWidget::init()
{
    int val = m_FindFund->FundForm().size();
    setButtonEnabled(true);
    QString str = QString("%1条记录").arg(val);
    m_Label->setText(str);
    //m_ButtonHidden->setEnabled(true);
}
void FundInfoDockWidget::ButtonFindFundSlots(QPushButton* button)
{
    if (!button->isEnabled())
        return;
    sendMessage("正在联网查询中，请等待");
    auto find = new FindFundNameThread(this);
    find->FindFundName(m_FindFund);
    setButtonEnabled(false);
    connect(find, &FindFundNameThread::ret, this,&FundInfoDockWidget::resultToTable);
    // 线程结束后，自动销毁
    connect(find, &FindFundNameThread::finished, find, &FindFundNameThread::deleteLater);
    find->start();
}
